//
//  SISSConfirmIntentionHouseViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/5.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSConfirmIntentionHouseViewController.h"
#import "SISSPassengerTakeLookDetailViewController.h"
#import "SISSImportantPassengerTakeLookRecordCell.h"
#import "SISSPassengerProgressViewController.h"
#import "SISSUploadSuccessViewController.h"

#import "SISSPassengerProgressModel.h"
#import "SISSPassengerTakeLookModel.h"
#import "SISSNetworkingManager+PassengerProgress.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSHousingCell.h"
#import "SISSHouseResourceDetailVC.h"

static NSString * const HousingCellID = @"HousingCellID";

@interface SISSConfirmIntentionHouseViewController ()<SISSImportantPassengerTakeLookRecordCellDelegate>

/* 带看模型数组 */
@property (strong, nonatomic) NSArray<SISSPassengerTakeLookModel *> *takeLookArr;

@property (strong, nonatomic) NSArray<SISSHouseResourceModel *> *houseResourceArr;

@end

@implementation SISSConfirmIntentionHouseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.hasConfirm ? @"确定购买房源" : @"选择确定购买的房源";
    if (self.hasConfirm) {
        // 注册cell
        [self.tableView registerNib:[UINib nibWithNibName:@"SISSHousingCell" bundle:nil] forCellReuseIdentifier:HousingCellID];
        [self requestTradeHouseInfoData];
    } else {
        [self requestPassengerTakeLookRecordData];
        WeakSelf
        [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
            kSISSRefresh.refreshType = SISSRefressTypeUpDown;
            [weakSelf upDownLoadData];
        }];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)confirmButtonDidClickAction {
    [super confirmButtonDidClickAction];
    
    SISSPassengerTakeLookModel *takeLookModel = self.takeLookArr[self.selectIndexPath.row];
    [self alertConfirmCacellActionAlertWithTitle:self.hasConfirm ? @"取消交易" : @"确定交易" message:self.hasConfirm ? @"您确定取消双方交易，并放弃后续已经开始的流程" : @"您确定双方已经达成一致购买意见并发起交易吗？" leftConfirmTitle:self.hasConfirm ? @"确定取消交易" : @"确定交易" rightConfirmTitle:@"取消" selectLeftBlock:^{
        if (self.hasConfirm) {
            
        } else {
            NSDictionary *parameters = @{
                                         @"ahbpId" : takeLookModel.ahbpId ? takeLookModel.ahbpId : @"",
                                         @"adbpId" : self.passengerProgressModel.adbpId,
                                         @"ahrId" : takeLookModel.ahrId,
                                         @"houseType" : @(takeLookModel.takelookType),
                                         };
            [kNetworkManager confirmTradeHouseWithParameters:parameters success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    SISSUploadSuccessViewController *vc = [[SISSUploadSuccessViewController alloc] init];
                    vc.title = self.hasConfirm ? @"取消交易" : @"选择交易客户";
                    vc.tipStr = self.hasConfirm ? @"交易已取消" : @"交易意向已锁定,请尽快完成合同签署";
                    vc.popBackClassType = SISSProgressPopBackClassTypePassenger;
                    [self.navigationController pushViewController:vc animated:YES];
                }

            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
        
    } selectRightBlock:^{
        
    }];

}



#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.hasConfirm ? self.houseResourceArr.count : self.takeLookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.hasConfirm) {
        SISSHousingCell *cell = [tableView dequeueReusableCellWithIdentifier:HousingCellID forIndexPath:indexPath];
        cell.isHideButton = YES;
        cell.houseResourceModel = self.houseResourceArr[indexPath.row];
        return cell;
    } else {
        SISSImportantPassengerTakeLookRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:importantPassengerTakeLookRecordCellReuseID forIndexPath:indexPath];
        cell.delegate = self;
        cell.takeLookModel = self.takeLookArr[indexPath.row];
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (self.hasConfirm) {
        SISSHouseResourceDetailVC *vc = [[SISSHouseResourceDetailVC alloc] init];
        vc.title = @"房源详情";
        vc.isHideBottomButton = YES;
        vc.houseResourceModel = self.houseResourceArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        SISSPassengerTakeLookDetailViewController *vc = [[SISSPassengerTakeLookDetailViewController alloc] init];
        vc.passengerProgressModel = self.passengerProgressModel;
        //    vc.brokerState = self.takeLookArr[indexPath.row].brokerState;
        vc.takeLookModel = self.takeLookArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark -- SISSImportantPassengerTakeLookRecordCellDelegate
- (BOOL)importantPassengerTakeLookRecordCell:(SISSImportantPassengerTakeLookRecordCell *)cell checkButtonIsSelected:(BOOL)isSelected {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.takeLookArr[self.selectIndexPath.row].isSelected = NO;
    self.takeLookArr[indexPath.row].isSelected = YES;
    self.selectIndexPath = indexPath;
    [self.tableView reloadData];
    self.isSelectMain = YES;
    
    return NO;
}

#pragma mark - 下拉属性
- (void)upDownLoadData {
    
    [self requestPassengerTakeLookRecordData];
}

#pragma mark - 获取客源带看记录数据
- (void)requestPassengerTakeLookRecordData {
    WeakSelf
    [kNetworkManager requestPassengerTakelookRecordWithAdbpId:self.passengerProgressModel.adbpId success:^(NSArray<SISSPassengerTakeLookModel *> *takeLookArr) {
        
        weakSelf.takeLookArr = takeLookArr;
        
        [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (takeLookArr.count * 110 > SCREEN_HEIGHT - kTopHeight - 120) {
                make.height.mas_equalTo(SCREEN_HEIGHT - kTopHeight - 120);
            } else {
                make.height.mas_equalTo(takeLookArr.count * 110);
            }
        }];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];
        
        
    } failed:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

- (void)requestTradeHouseInfoData {
    
    WeakSelf
    [kNetworkManager requestTradeHouseInfoWithAdbpId:self.passengerProgressModel.adbpId success:^(SISSHouseResourceModel *houseInfoModel) {
        
        weakSelf.houseResourceArr = @[houseInfoModel];
        
        [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1 * 66);
        }];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
